using FishNet.Managing;
using GameBasic;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace VCity
{
    public class LoginUI : MonoBehaviour
    {
        [Space]
        public TMP_InputField nameField;
        public Button enterBtn;

        [Space]
        public Button previousBtn;
        public Button nextBtn;
        public SkinnedMeshRenderer charRender;
        int meshIndex;

        [Space]
        public GameObject developerInfo;
        public Button helpBtn;

        CursorState cursorState;

        private void Awake()
        {
            cursorState = new CursorState();
            cursorState.visible = true;
            cursorState.lockMode = CursorLockMode.None;

            // buttons
            previousBtn.onClick.AddListener(OnPreviousBtn);
            nextBtn.onClick.AddListener(OnNextBtn);
            enterBtn.onClick.AddListener(OnEnterBtn);

            helpBtn.onClick.AddListener(OnHelpBtn);
        }

        private void Start()
        {
            CheckMesh();
        }

        private void OnEnable()
        {
            if (InputMain.Instance != null)
                InputMain.Instance.AddCursorState(cursorState);
        }

        private void OnDisable()
        {
            if (InputMain.Instance != null)
                InputMain.Instance.RemoveCursorState(cursorState);
        }

        void OnEnterBtn()
        {
            // disable
            enterBtn.interactable = false;

            //
            var netMain = NetworkMain.Instance;
            StartCoroutine(netMain.Client_TryLogin(new NetLoginData()
            {
                username = nameField.text,
                skin = (byte)meshIndex
            }, success => 
            {
                if (success)
                {
                    ClientMain.Instance.GoGameplay(meshIndex);
                }
                else
                {
                    if (ClientMain.Instance.PlayOffline(meshIndex))
                    {
                        Debug.Log("Offline mode");
                    }
                    else
                    {
                        Debug.Log("Offline not allowed");
                    }
                }

                enterBtn.interactable = true;
            }));
        }

        void OnPreviousBtn()
        {
            meshIndex--;
            CheckMesh();
        }

        void OnNextBtn()
        {
            meshIndex++;
            CheckMesh();
        }

        void OnHelpBtn()
        {
            developerInfo.SetActive(!developerInfo.activeSelf);
        }

        void CheckMesh()
        {
            var rs = GameResources.Instance;
            var meshes = rs.characterMeshes;
            if (meshIndex >= meshes.Length)
                meshIndex = 0;
            else if (meshIndex < 0)
                meshIndex = meshes.Length - 1;

            if (meshes.IsLegalIndex(meshIndex))
                charRender.sharedMesh = meshes[meshIndex];
        }
    }
}